package com.cro.prison.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cro.prison.entity.pojo.Prisoner;
import com.cro.prison.entity.vo.AddPrisonerVo;
import com.cro.prison.entity.vo.BehaviorCountVo;
import com.cro.prison.service.DailyBehaviorService;
import com.cro.prison.service.PrisonService;
import com.cro.prison.utils.Code;
import com.cro.prison.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Arrays;


import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/prisoner")
public class PrisonerController {

    //查看三种罪犯名单

    @Autowired
    PrisonService prisonService;
    @Autowired
    DailyBehaviorService dailyBehaviorService;

    @GetMapping("/analysisAndPrediction/{id}")
    @ResponseBody
    public Result analysisAndPrediction(@PathVariable Long id) {
        String analyseInfo = prisonService.analysisAndPrediction(id);
        return new Result(200, analyseInfo);
    }

    @GetMapping("/list/{prisonerType}")
    public Result listPrisonersByPrisonerType(@PathVariable Integer prisonerType) {
        List<Prisoner> prisonList = prisonService.list(new QueryWrapper<Prisoner>().eq("type", prisonerType));

        String s = prisonerType.toString();
        return new Result(200, prisonList, s);
    }

    @GetMapping("/listCount/{prisonerType}")
    public Result listPrisonersByPrisonerTypeCount(@PathVariable Integer prisonerType) {
        List<Integer> list = prisonService.selectByType(prisonerType);//改成 日常行为

        return new Result(200, list);
    }

    @GetMapping("/listBehaviorCount/{prisonerType}")
    public Result listBehaviorCount(@PathVariable Integer prisonerType) {
        List<BehaviorCountVo> list = dailyBehaviorService.selectByType(prisonerType);
        return new Result(200, list);
    }

    @GetMapping("/listBehaviorCount/queryByDate")//新进罪犯的日常行为统计
    public Result listBehaviorCountByDate() {
        List<BehaviorCountVo> list = dailyBehaviorService.selectByTypeByDate();
        return new Result(200, list);
    }

    @GetMapping("/listAllBehaviorCount")//所有罪犯的日常行为统计
    public Result listAllBehaviorCount() {
        List<BehaviorCountVo> list = dailyBehaviorService.selectAllBehaviorCount();
        return new Result(200, list);
    }



    @GetMapping("/list/queryByDate")
    public Result listPrisonersByDate() {
        //新进罪犯名单
        List<Prisoner> prisonList = prisonService.listByDate();
        return new Result(200, prisonList, "3");
    }

    @GetMapping("/listCount/queryByDate")
    public Result listPrisonersByDateCount() {
        //新进罪犯名单
        List<Prisoner> prisonList = prisonService.listByDate();
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        for (Prisoner prisoner : prisonList) {
            switch (prisoner.getMood()) {
                case 1:
                    a++;
                    break;
                case 2:
                    b++;
                    break;
                case 3:
                    c++;
                    break;
                case 4:
                    d++;
                    break;
                case 5:
                    e++;
                    break;
            }

        }
        List<Integer> list = Arrays.asList(a, b, c, d, e);
        return new Result(200, list);
    }

    @GetMapping("/getTime")
    @ResponseBody
    public String listPrisonersByPrisonerType() {
        //类型中 新进犯人，在调用接口时进行更新
        Date date = new Date();
        //当前时间减半年
        return "time:" + (date.getTime() - 183 * 24 * 60 * 60 * 1000);
    }

    //犯人行为分析，创建犯人信息
    @PostMapping("/addPrisoner")
    @ResponseBody
    public Result addPrisoner(@RequestBody AddPrisonerVo prisoner) throws ParseException {

        if (prisonService.getPrisonerByIdNumber(prisoner.getIdNumber())!=null) {
            return new Result(Code.SAVE_ERR);
        }
        
        prisonService.addPrisoner(prisoner);

        return new Result(Code.SAVE_OK);
    }


}
